import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/transaction_flow_details_entity.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/clipboard_utils.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/left_right_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'transaction_flow_details_controller.dart';

class TransactionFlowDetailsPage extends StatelessWidget {
  final controller = Get.put(TransactionFlowDetailsController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "订单详情"),
      body: GetBuilder<TransactionFlowDetailsController>(
        builder: (controller) {
          return MultiStateWidget(
            child: buildContentWidget(),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }

  Widget buildContentWidget() {
    if (controller.entity == null) {
      return Container();
    }
    TransactionFlowDetailsEntity entity = controller.entity!;
    return SingleChildScrollView(
      physics: BouncingScrollPhysics(),
      child: Column(
        children: [
          Container(
            height: 76.w,
            margin: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
            decoration: BoxDecoration(
              color: Colours.color_white,
              borderRadius: BorderRadius.circular(10.w),
            ),
            child: Stack(
              alignment: AlignmentDirectional.center,
              children: [
                Row(
                  children: [
                    Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            FormatUtils.formatMoney(entity.orderQuantity),
                            style: TextStyle(
                              color: Colours.color_text_theme,
                              fontSize: 22.sp,
                              fontWeight: FontWeight.w500,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          SizedBox(
                            height: 5.w,
                          ),
                          Text(
                            "加油数量(升)",
                            style: TextStyle(
                              color: Colours.color_6f7082,
                              fontSize: 12.sp,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            FormatUtils.formatMoney(entity.orderAmount),
                            style: TextStyle(
                              color: Colours.color_text_theme,
                              fontSize: 22.sp,
                              fontWeight: FontWeight.w500,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          SizedBox(
                            height: 5.w,
                          ),
                          Text(
                            "金额(元)",
                            style: TextStyle(
                              color: Colours.color_6f7082,
                              fontSize: 12.sp,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            FormatUtils.formatMoney(entity.price),
                            style: TextStyle(
                              color: Colours.color_text_theme,
                              fontSize: 22.sp,
                              fontWeight: FontWeight.w500,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          SizedBox(
                            height: 5.w,
                          ),
                          Text(
                            "单价(元)",
                            style: TextStyle(
                              color: Colours.color_6f7082,
                              fontSize: 12.sp,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Visibility(
                  visible: (entity.valid ?? 0) == 0,
                  child: Positioned(
                    right: 16.w,
                    child: Image.asset("images/ic_invalid.png"),
                  ),
                ),
              ],
            ),
          ),
          LeftRightText(
            textLeft: "客户名称",
            textRight: entity.customersName,
          ),
          LeftRightText(
            textLeft: "加油车牌",
            textRight: entity.vehicleNumber,
          ),
          LeftRightText(
            textLeft: "加油油品",
            textRight: entity.oilName,
          ),
          LeftRightText(
            textLeft: "加油时间",
            textRight: entity.tradingTime,
          ),
          SizedBox(
            height: 12.w,
          ),
          LeftRightText(
            textLeft: "司机姓名",
            textRight: entity.driverName,
          ),
          LeftRightText(
            textLeft: "司机电话",
            textRight: entity.driverPhone,
          ),
          LeftRightText(
            textLeft: "司机手写签名",
            textRight: (entity.signatureImage ?? "").isNotEmpty ? "查看" : "",
            onTap: () {
              previewPhoto(entity.signatureImage);
            },
          ),
          SizedBox(
            height: 12.w,
          ),
          LeftRightText(
            textLeft: "车辆里程数(km)",
            textRight: (entity.vehicleMileage ?? 0.0) == 0.0 ? "" : (entity.vehicleMileage ?? 0.0).toStringAsFixed(2),
          ),
          LeftRightText(
            textLeft: "里程表照片",
            textRight: (entity.vehicleMileageImage ?? "").isNotEmpty ? "查看" : "",
            onTap: () {
              previewPhoto(entity.vehicleMileageImage);
            },
          ),
          LeftRightText(
            textLeft: "加注站小票照片",
            textRight: (entity.joinInSmallTicketImage ?? "").isNotEmpty ? "查看" : "",
            onTap: () {
              previewPhoto(entity.joinInSmallTicketImage);
            },
          ),
          LeftRightText(
            textLeft: "油枪显示屏照片",
            textRight: (entity.gunScreenImage ?? "").isNotEmpty ? "查看" : "",
            onTap: () {
              previewPhoto(entity.gunScreenImage);
            },
          ),
          LeftRightText(
            textLeft: "小票凭证",
            textRight: (entity.smallTicketImage ?? "").isNotEmpty ? "查看" : "",
            onTap: () {
              previewPhoto(entity.smallTicketImage);
            },
          ),
          Container(
            color: Colours.color_white,
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
              child: Row(
                children: [
                  Text(
                    "交易流水号",
                    style: TextStyle(
                      color: Colours.color_6f7082,
                      fontSize: 14.sp,
                    ),
                  ),
                  SizedBox(
                    width: 10.w,
                  ),
                  Expanded(
                    child: Text(
                      entity.tradingSerialNumber ?? "",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                      ),
                      textAlign: TextAlign.right,
                    ),
                  ),
                  SizedBox(
                    width: (entity.tradingSerialNumber ?? "").isNotEmpty ? 8.w : 0,
                  ),
                  Visibility(
                    visible: (entity.tradingSerialNumber ?? "").isNotEmpty,
                    child: GestureDetector(
                      onTap: () {
                        ClipboardUtils.setData(entity.tradingSerialNumber);
                        ToastUtils.toast("已复制到粘贴板");
                      },
                      child: Image.asset("images/ic_copy.png"),
                    ),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(
            height: 12.w,
          ),
          Container(
            color: Colours.color_white,
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
              child: Row(
                children: [
                  Text(
                    "站点名称",
                    style: TextStyle(
                      color: Colours.color_6f7082,
                      fontSize: 14.sp,
                    ),
                  ),
                  SizedBox(
                    width: 10.w,
                  ),
                  Expanded(
                    child: Text(
                      entity.stationName ?? "",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                      ),
                      textAlign: TextAlign.right,
                    ),
                  ),
                  SizedBox(
                    width: (entity.stationName ?? "").isNotEmpty ? 16.w : 0,
                  ),
                  Visibility(
                    visible: (entity.stationName ?? "").isNotEmpty,
                    child: TextButtonWithIcon(
                      onPressed: () {},
                      icon: Image.asset("images/ic_navigation.png"),
                      label: Text(
                        "导航",
                        style: TextStyle(
                          color: Colours.color_34c759,
                          fontSize: 12.sp,
                        ),
                      ),
                      style: ButtonStyle(
                        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        minimumSize: MaterialStateProperty.all(Size(0, 0)),
                        padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                      ),
                      textIconPadding: 3.w,
                      iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                    ),
                  ),
                ],
              ),
            ),
          ),
          LeftRightText(
            textLeft: "站点地址",
            textRight: entity.stationAddressDetail,
          ),
          LeftRightText(
            textLeft: "油枪编号",
            textRight: entity.greaseGunNo,
          ),
          SizedBox(
            height: 12.w,
          ),
          LeftRightText(
            textLeft: "加油员姓名",
            textRight: entity.operator,
          ),
          LeftRightText(
            textLeft: "联系电话",
            textRight: entity.contactsPhone,
          ),
          SizedBox(
            height: 12.w,
          ),
          Visibility(
            visible: (entity.valid ?? 0) == 0,
            child: Column(
              children: [
                LeftRightText(
                  textLeft: "作废操作人",
                  textRight: entity.validOperatorName,
                ),
                LeftRightText(
                  textLeft: "作废时间",
                  textRight: entity.validTime,
                ),
                SizedBox(
                  height: 12.w,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  void previewPhoto(String? path) {
    if (path == null || path.isEmpty) {
      return;
    }
    Get.toNamed(Routes.previewPhoto, arguments: {
      Constant.PREVIEW_PHOTO_TYPE: Constant.PREVIEW_PHOTO_TYPE_NETWORK,
      Constant.PREVIEW_PHOTO_IMAGE_LIST: [path],
      Constant.PREVIEW_PHOTO_INDEX: 0,
      Constant.PREVIEW_PHOTO_IS_SHOW_INDEX: false,
      Constant.PREVIEW_PHOTO_IS_DOWNLOAD_IMAGE: false,
    });
  }
}
